<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>门岗管理</title>
    <!-- Layui -->
    <link rel="stylesheet" href="./layui/css/layui.css"/>
    <script src="./layui/layui.js"></script>
    <link rel="stylesheet" href="css/admin.css">
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        // 全局配置
        window.appConfig = {
            //apiBaseUrl: 'http://localhost:8080' // 开发环境
            apiBaseUrl: 'http://59.110.144.74:8080' // 生产环境
        };
    </script>

</head>
<body>

<div class="content-container" style="padding: 20px;">
    <form class="layui-form" lay-filter="gateForm">
        <input type="hidden" name="id" id="gateId">
        <div class="layui-form-item">
            <label class="layui-form-label">门岗名称</label>
            <div class="layui-input-block">
                <input type="text" name="gateName" id="gateName" required lay-verify="required" placeholder="请输入门岗名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="saveGate">保存</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="window.history.back()">返回</button>
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['form', 'layer'], function () {
        const $ = layui.$;
        const layer = layui.layer;

        // 获取 URL 参数
        const urlParams = new URLSearchParams(window.location.search);
        const mode = urlParams.get('mode');
        const id = urlParams.get('id');
        const gateName = urlParams.get('gateName');

        if (mode === 'edit') {
            $('#gateId').val(id);
            $('#gateName').val(gateName);
        }

        // 提交门岗数据
        layui.form.on('submit(saveGate)', function(obj) {
            const formData = obj.field;

            let url, method;
            if (formData.id && formData.id !== '') {
                url = window.appConfig.apiBaseUrl +'/gate';
                method = 'PUT';
            } else {
                url = window.appConfig.apiBaseUrl +'/gate';
                method = 'POST';
            }

            $.ajax({
                url: url,
                method: method,
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res) {
                    if (res.code === 0) {
                        layer.msg('操作成功', { icon: 1 });
                        setTimeout(() => {
                            window.location.href = 'admin.html#gate'; // 返回上一页
                        }, 1000);
                    } else {
                        layer.msg('操作失败', { icon: 2 });
                    }
                },
                error: function(xhr) {
                    layer.msg('服务器错误: ' + xhr.statusText, { icon: 2 });
                }
            });

            return false; // 阻止默认提交行为
        });
    });
</script>

</body>
</html>
